<?php

class CafeApiAction extends Action {
	
    public function index() {
    	echo strtotime(date("Y-m-d"));exit;
		$this->display();
    }
    
    //用户签到
    public function sign(){
    	if(empty($_REQUEST['user_id'])||empty($_REQUEST['order_id'])){
    		$this->ajaxReturn(null,'缺少参数','-1001');
    	}
    	$data = array();
    	$data['order_id'] = $_REQUEST['order_id'];
    	$data['sign_time'] = time();
    	//上传图片
    	if (!empty($_FILES)) {
    		$file = (array) uploadFile(300, 300);
    		for ($i = 0; $i < count($file); $i++){
    			if($file[$i]['key']=="cafPath"){  //音频
    				$data['image'][]=$file[$i]['savepath'] . $file[$i]['savename'];
    			}else{
    				$data['image'][] =$file[$i]['savepath'] . "m_" . $file[$i]['savename'];
    			}
    		}
    		$data['sign_img'] = json_encode($data['image']);    //签到图片
    	}else{
    		$data['sign_img'] = 0;    //签到图片
    	}
    	if($_REQUEST['source']==2){
    		$data['yy_date'] = strtotime(date("Y-m-d"));
    		$H = date('H');
    		if($H>=9 && $H<=11){
    			$data['yy_time'] = 1;
    		}elseif($H>=11 && $H<=13){
    			$data['yy_time'] = 2;
    		}elseif($H>=13 && $H<=15){
    			$data['yy_time'] = 3;
    		}elseif($H>=15 && $H<=17){
    			$data['yy_time'] = 4;
    		}elseif($H>=17 && $H<=19){
    			$data['yy_time'] = 5;
    		}elseif($H>=19 && $H<=21){
    			$data['yy_time'] = 6;
    		}
    		$data['status'] = 5;
    		$res = M('order')->save($data);
    		$re = array();
    		$re['order_id'] = $res;
    		$re['yy_date'] = $data['yy_date'];
    		$re['yy_time'] = $data['yy_time'];
    		$re['two_code'] = @C(WEB_URL).'Uploads/JsCode/qbyjs001.jpg';
    		$this->ajaxReturn($re,'签到成功','1');
    	}
    	$res = M('order')->save($data);
    	if($res){
    		$this->ajaxReturn(null,'签到成功',1);
    	}else{
    		$this->ajaxReturn(null,'签到失败','-1');
    	}
    }

    //顾客列表
    public function CustomList(){
    	$Order = M('order');

        $where="(hx_order.source=4 and yy_date is not null) or (hx_order.source in (1,2))";
    	$orderlist = $Order->field("hx_user.user_id,hx_user.username,hx_user.realname,hx_user.sex,hx_order.order_id,hx_order.order_name,hx_order.js_id,hx_order.pay_type,hx_order.yy_date,hx_order.yy_time,hx_order.start_time,hx_order.end_time,hx_order.sign_time,hx_order.create_time,hx_order.status")->where($where)->join('hx_user on hx_order.user_id=hx_user.user_id')->page('1,20')->order('hx_order.create_time desc')->select();
    	 
    	$Cafe = M('cafe_order');
    	$cafelist = $Cafe->field("hx_user.username,hx_user.realname,hx_user.sex,hx_cafe_order.*")->join('hx_user on hx_cafe_order.user_id=hx_user.user_id')->page('1,10')->order('hx_cafe_order.create_time desc')->select();

    	$time = array();
    	foreach($orderlist as $list){
    		$time[$list['create_time']] = $list['user_id'];
    	}
    	foreach($cafelist as $list){
    		$time[$list['create_time']] = $list['user_id'];
    	}
    	krsort($time);
    	$time = array_flip($time);
    	$data = array();
    	$num = 0;
    	foreach ($time as $key=>$value){
    		$data[$num]['order'] = null;
    		$data[$num]['cafe'] = null;
    		foreach($orderlist as $list){
    			if($key==$list['user_id']){
    				$data[$num]['user_id'] = $list['user_id'];
    				$data[$num]['username'] = $list['username'];
    				$data[$num]['realname'] = $list['realname'];
    				$data[$num]['sex'] = $list['sex'];
    				$data[$num]['order']['order_id'] = $list['order_id'];
    				$data[$num]['order']['order_name'] = $list['order_name'];
    				$data[$num]['order']['js_id'] = $list['js_id'];
    				$data[$num]['order']['pay_type'] = $list['pay_type'];
    				$data[$num]['order']['yy_date'] = $list['yy_date'];
    				$data[$num]['order']['yy_time'] = $list['yy_time'];
    				$data[$num]['order']['create_time'] = $list['create_time'];
    				$data[$num]['order']['sign_time'] = $list['sign_time'];
    				$data[$num]['order']['start_time'] = $list['start_time'];
    				$data[$num]['order']['end_time'] = $list['end_time'];
    			}
    		}
    		foreach($cafelist as $list){
    			if($key==$list['user_id']){
    				$data[$num]['user_id'] = $list['user_id'];
    				$data[$num]['username'] = $list['username'];
    				$data[$num]['realname'] = $list['realname'];
    				$data[$num]['sex'] = $list['sex'];
    				$data[$num]['cafe']['co_id'] = $list['co_id'];
    				$data[$num]['cafe']['user_id'] = $list['user_id'];
    				$data[$num]['cafe']['cafe_id'] = $list['cafe_id'];
    				$data[$num]['cafe']['cafe_name'] = $list['cafe_name'];
    				$data[$num]['cafe']['create_time'] = $list['create_time'];
    				$data[$num]['cafe']['status'] = $list['status'];
    			}
    		}
    		$num++;
    	}

    	if($data){
    		$this->ajaxReturn($data,'成功',1);
    	}else{
    		$this->ajaxReturn(null,'没有数据',-1);
    	}
    }
    
    //顾客详情
    public function CustomDetail(){
    	if(empty($_REQUEST['user_id'])||(empty($_REQUEST['order_id'])&&empty($_REQUEST['co_id']))){
    		$this->ajaxReturn(null,'缺少参数','-1001');
    	}
    	$data = array();
    	$user = M('user')->field("user_id,username,realname,tel,sex,open_id")->find($_REQUEST['user_id']);
    	$data['user'] = $user;
    	$data['order'] = null;
    	$data['cafe'] = null;
    	if($_REQUEST['order_id']){
    		$order = M('order')->find($_REQUEST['order_id']);
    		$orderInfo = M('order_info')->where("order_id=".$_REQUEST['order_id'])->select();
    		for($i=0;$i<count($orderInfo);$i++){
    			$orderInfo[$i]['logo'] = @C(WEB_URL).$orderInfo[$i]['logo'];
    		}
    		$order['paycode'] = 'http://wap.99qby.com/pay/alipay/order_id/'.$order['order_id'];
    		$data['order'] = $order;
    		$data['order']['pjlist'] = $orderInfo;
    	}
    	if($_REQUEST['co_id']){
    		$cafe = M('cafe_order')->find($_REQUEST['co_id']);
    		$data['cafe'] = $cafe;
    	}
    	$this->ajaxReturn($data,'查询成功',1);
    }
    
    //查询饮料
    function cafeInfo(){
    	if(empty($_REQUEST['code'])){
    		$this->ajaxReturn('','缺少参数',-1001);
    	}
    
    	$maps['code'] = $_REQUEST['code'];
    	$Info = M('cafe')->where($maps)->find();
    
    	if($Info){
    		//$Info['logo'] = @C(WEB_URL).$Info['logo'];
    		$this->ajaxReturn($Info,'成功',1);
    	}
    	$this->ajaxReturn(null,'查询成功',0);
    }
    
    //修改饮料
    function editCafe(){
    	if(empty($_REQUEST['co_id'])||empty($_REQUEST['cafe_id'])){
    		$this->ajaxReturn('','缺少参数',-1001);
    	}

        $cafe_name=M("cafe")->where("cafe_id=".$_REQUEST['cafe_id'])->getField("cafe_name");
    	
        $data['co_id'] = $_REQUEST['co_id'];
    	$data['cafe_name'] = $cafe_name;
        $data['cafe_id'] = $_REQUEST['cafe_id'];
        $data['status'] = 2;

        $Model = M('cafe_order');
    	$res = $Model->save($data);
    	if($res){
    		$this->ajaxReturn($_REQUEST['co_id'],'成功',1);
    	}else{
    		$this->ajaxReturn(null,'失败',0);
    	}
    }
    
    //用户点单
    function addCafe(){
    	if(empty($_REQUEST['user_id'])||empty($_REQUEST['cafe_id'])){
    		$this->ajaxReturn('','缺少参数',-1001);
    	}
    	$cafe = M('cafe')->find($_REQUEST['cafe_id']);
    	$data['cafe_id'] = $_REQUEST['cafe_id'];
    	$data['user_id'] = $_REQUEST['user_id'];
    	$data['cafe_name'] = $cafe['cafe_name'];
    	$data['count'] = 1;
    	$data['code'] = $cafe['code'];
    	$data['create_time'] = time();
    	if($_REQUEST['source']==2){
    		$data['status'] = 2;
    	}
    	
    	$res = M('cafe_order')->add($data);
    	if($res){
    		$this->ajaxReturn($res,'成功',1);
    	}else{
    		$this->ajaxReturn(null,'失败',0);
    	}
    }
    
    //查询消息列表
    function msgList(){
    	/* if(empty($_REQUEST['code'])){
    		$this->ajaxReturn('','缺少参数',-1001);
    	} */
    	$list = M('cafe_order')->field("hx_user.username,hx_user.realname,hx_user.sex,hx_cafe_order.*")->join('hx_user on hx_cafe_order.user_id=hx_user.user_id')->page(1,20)->order("hx_cafe_order.create_time desc")->select();
    	$data = array();
    	$data['new'] = null;
    	$data['old'] = null;
    	if($list){
    		foreach ($list as $li){
    			if($li['status']==1){
    				$data['new'][] = $li;
    			}else{
    				$data['old'][] = $li;
    			}
    		}
    		$this->ajaxReturn($data,'成功',1);
    	}
    	$this->ajaxReturn($data,'查询成功',0);
    }
    
    //发送短信
    function sendMsg(){
    	if(empty($_REQUEST['order_id'])){
    		$this->ajaxReturn('','缺少参数',-1001);
    	}
    	
    	$map['order_id'] = $_REQUEST['order_id'];
    	$Model = M('order');
    	$order = $Model->join('hx_user u on hx_order.user_id=u.user_id')->field('hx_order.*,u.username,u.realname,u.sex')->where($map)->find();
    	
    	$sex = '女士';
    	if($order['sex']==1){
    		$sex = '先生';
    	}
    	$js = 'Sam(正男)13019878305';
    	if($order['js_id']==2){
    		$js = 'Neal(苏楠)13770738369';
    	}
    	import("@.ORG.MobileSms");
    	$mobilesms = new MobileSms();
    	$sms['to_phone'] = $order['username'];
    	$sms['content'] = "尊敬的".$order['realname'].$sex.",您好!您的爱车已经开始保养了,您可以关注微信号[去保养],点击保养直播在线观看保养过程.如有疑问,请联系责任技师".$js.",谢谢!";
    	
    	$rs = $mobilesms->send($sms['to_phone'], $sms['content']);
    	$this->ajaxReturn($rs,'查询成功',1);
    }
    

    //保存配件
    function savePj(){
    	/* $pj="W930/21,W719/30,W719/45,W712/94,HU722z,HU831x,HU8001x,HU7005x,HU8010Z,W712/90,HU932/6n,HU719/6x,W719/45,HU719/6x,HU719/7x,HU816zKIT,HU816x,HU925/4x,HU715/5X,HU7003x,HU815/2x,HU816x,HU8002xKIT,HU816zKIT,HU816x,HU816zKIT,HU8002xKIT,HU9254X,HU8007z,HU823x,HU8005z,HU514y,HU711/6z,HU612/1x,HU718/5x,HU514x,W610/3,W610/6,HU711/51X,W68/1,W68/3,W713/29,HU925-4y,W811/80,W71290,W71945,W71290,W71945,W71930,HU7262X,W712/92,W7159,W719/45,W712/90,HU7196X,HU6007,W712/22,HU612/2X";
    	 $Arr = explode(",",$pj);
    	$Arr = array_unique($Arr);
    	$num = 0;
    	$jl = '';
    	foreach ($Arr as $key=>$value){
    	$map['pj_xinghao'] = array('like','%'.$value.'%');;
    	$pj = M('peijian')->where($map)->find();
    	$data = array();
    	if(!$pj){
    	$jl .= ','.$value;$num++;
    	/* $data['type_id'] = 2;
    	$data['type_name'] = '机油滤清器';
    	$data['logo'] = 'Public/images/baoyang/m_mann.jpg';
    	}
    	}
    	echo $jl;echo $num;exit;
    	$this->ajaxReturn($rs,'查询成功',1); */
    	$list = M('jl_cl')->field("model_id,man,male,boshi")->select();
    	$jl = M('jl');
    	$pj = M('peijian');
    	foreach ($list as $li){
    		$data['model_id'] = $li['model_id'];
    		$data['G'] = $Info['pj_id'];
    		$data['H'] = '机油滤清器';
    		if($li['man']){
    			$map['pj_xinghao'] = $li['man'];
    			$Info = $pj->where($map)->field("pj_id")->find();
    			if($Info){
    				$data['G'] = $li['man'];
    				$data['pj_id'] = $Info['pj_id'];
    				$jl->add($data);
    			}
    		}
    		if($li['male']){
    			$map['pj_xinghao'] = $li['male'];
    			$Info = $pj->where($map)->find();
    			if($Info){
    				$data['G'] = $li['male'];
    				$data['pj_id'] = $Info['pj_id'];
    				$jl->add($data);
    			}
    		}
    		if($li['boshi']){
    			$map['pj_xinghao'] = $li['boshi'];
    			$Info = $pj->where($map)->find();
    			if($Info){
    				$data['G'] = $li['boshi'];
    				$data['pj_id'] = $Info['pj_id'];
    				$jl->add($data);
    			}
    		}
    	}
    }
    
    
}

?>